package com.sgb.kjwl.view.ui.fragment.enterprise;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.FragmentServiceQuoteBinding;
import com.sgb.kjwl.di.enterprise.DaggerEnterpriseFragComponent;
import com.sgb.kjwl.di.enterprise.EnterpriseFragModule;
import com.sgb.kjwl.model.entity.enterprise.ServiceQuoteEntity;
import com.sgb.kjwl.utils.ActivityJump;
import com.sgb.kjwl.view.ui.activity.enterprise.ProfessionalTeamDetailActivity;
import com.sgb.kjwl.view.ui.activity.enterprise.ServiceQuoteListActivity;
import com.sgb.kjwl.view.ui.adapter.enterprise.ServiceQuoteAdapter;
import com.sgb.kjwl.viewmodel.enterprise.ServiceQuoteFragModel;
import com.swgk.core.base.di.AppComponent;

import java.util.List;

import javax.inject.Inject;

public class PsTeamDetailServiceQuoteFragment extends AppLazyFragment {

    @Inject
    ServiceQuoteFragModel model;

    private FragmentServiceQuoteBinding binding;
    String teamNo = "";
    ServiceQuoteAdapter serviceQuoteAdapter;

    public static PsTeamDetailServiceQuoteFragment newInstance(String no) {
        PsTeamDetailServiceQuoteFragment fragment = new PsTeamDetailServiceQuoteFragment();
        Bundle bundle = new Bundle();
        bundle.putString("teamNo", no);
        fragment.setArguments(bundle);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        DaggerEnterpriseFragComponent.builder()
                .appComponent(appComponent)
                .enterpriseFragModule(new EnterpriseFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_service_quote, container, false);
        ((ProfessionalTeamDetailActivity) getActivity()).setChildObjectForPosition(binding.getRoot(), 3);
        if (serviceQuoteAdapter == null)
            serviceQuoteAdapter = new ServiceQuoteAdapter(getContext(), null);

        LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
        binding.recyclerView.setLayoutManager(layoutManager);
        binding.recyclerView.setAdapter(serviceQuoteAdapter);
        binding.recyclerView.setLoadingMoreEnabled(false);
        binding.recyclerView.setPullRefreshEnabled(false);
        binding.tvShowMore.setVisibility(View.GONE);
        binding.tvShowMore.setOnClickListener(view -> ActivityJump.jumpToActivity(getContext(), ServiceQuoteListActivity.class, Pair.create("teamNo", teamNo)));
        return binding.getRoot();
    }


    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    protected void onLazyLoad() {
        teamNo = getArguments().getString("teamNo");
        model.getServiceQuoteQueryList(false, teamNo);
    }


    /**
     * 服务报价数据
     *
     * @param list
     * @param isLoadMore
     */
    public void showList(List<ServiceQuoteEntity> list, boolean isLoadMore) {
        binding.recyclerView.refreshComplete();
        if (isLoadMore) {
            if (null != list && list.size() > 0) {
                binding.recyclerView.loadMoreComplete();
                serviceQuoteAdapter.addDatas(list);
            } else {
                binding.recyclerView.setNoMore(true);
            }
            binding.recyclerView.setVisibility(View.VISIBLE);
        } else {
            binding.recyclerView.refreshComplete();
            if (null != list && list.size() > 0) {
                binding.recyclerView.setVisibility(View.VISIBLE);
                binding.emptyView.setVisibility(View.GONE);
                serviceQuoteAdapter.setDatas(list);
                binding.recyclerView.setLoadingMoreEnabled(false);

                binding.tvShowMore.setVisibility(list.size() >= 3 ? View.VISIBLE : View.GONE);

            } else {
                serviceQuoteAdapter.setDatas(null);
                binding.recyclerView.setVisibility(View.GONE);
                binding.emptyView.setVisibility(View.VISIBLE);
            }
        }
    }
}
